package com.example.idachuappone.index.item;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.adapter.AdapterYouHui;
import com.example.idachuappone.index.ui.MyListView;
import com.example.idachuappone.person.entity.QuanResults;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class YouHuiMaActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	private AdapterYouHui adapterYouHui;
	private Button btn_back;
	private Button btn_index;
	private Dialog dialogAddQuan;
	private Dialog dialogLoading;
	private EditText et_quan_ma;
	private int index_item;
	private List<QuanResults> list;
	private LinearLayout ll_no_data;
	private MyListView lv_quan_ma;
	private String order_id;
	private QuanResults quanResults;

	public YouHuiMaActivity() {
		this.index_item = -1;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_you_hui_ma);
		this.index_item = getIntent().getIntExtra("index_item", -1);
		this.order_id = getIntent().getStringExtra("order_id");
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		initView();
		initData();
	}

	private void initData() {
		if (CheckUtil.checkNet(this)) {
			this.dialogLoading.show();
			NetUtil.get(this, new StringBuilder(Constant.MYYOUHUIMAORDERID).append(this.order_id).toString(), 100,
					new RequestCallBack<String>() {
						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							YouHuiMaActivity.this.dialogLoading.dismiss();
							String json = NetUtil.unescapeUnicode(responseInfo.result);
							try {
								YouHuiMaActivity.this.list = new ArrayList();
								JSONObject jsonObject = new JSONObject(json);
								if (jsonObject.getInt("code") == 10000) {
									JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("results");
									for (int i = 0; i < jsonArray.length(); i++) {
										QuanResults quanResults = new QuanResults();
										quanResults.parseJson(jsonArray.getJSONObject(i));
										YouHuiMaActivity.this.list.add(quanResults);
									}
									YouHuiMaActivity.this.initAdapter(YouHuiMaActivity.this.index_item);
									return;
								}
								MainToast.show(YouHuiMaActivity.this, jsonObject.getString("error"), 0);
							} catch (Exception e) {
								e.printStackTrace();
								MainToast.show(YouHuiMaActivity.this, "\u6682\u65e0\u4f18\u60e0\u7801!", 0);
								YouHuiMaActivity.this.ll_no_data.setVisibility(0);
							}
						}

						@Override
						public void onFailure(HttpException arg0, String arg1) {
							YouHuiMaActivity.this.dialogLoading.dismiss();
							MainToast.show(YouHuiMaActivity.this,
									YouHuiMaActivity.this.getResources().getString(R.string.netFail), 0);
						}
					});
			return;
		}
		MainToast.show(this, getResources().getString(R.string.txt_network), 0);
	}

	private void initView() {
		this.lv_quan_ma = (MyListView) findViewById(R.id.lv_quan_ma);
		this.adapterYouHui = new AdapterYouHui(this);
		this.lv_quan_ma.setAdapter(this.adapterYouHui);
		this.lv_quan_ma.setOnItemClickListener(this);
		this.ll_no_data = (LinearLayout) findViewById(R.id.ll_no_data);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setOnClickListener(this);
	}

	private void initAdapter(int index) {
		if (this.list == null || this.list.size() <= 0) {
			this.ll_no_data.setVisibility(0);
			return;
		}
		this.adapterYouHui.setList(this.list);
		this.adapterYouHui.notifyDataSetChanged();
		this.ll_no_data.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		this.index_item = position;
		Intent data = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable("quanResults", this.list.get(this.index_item));
		bundle.putInt("index", this.index_item);
		data.putExtras(bundle);
		setResult(-1, data);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_index /* 2131165213 */:
			this.dialogAddQuan = getAddQuanDialog();
			this.dialogAddQuan.setCanceledOnTouchOutside(false);
			this.dialogAddQuan.show();
		default:
		}
	}

	private Dialog getAddQuanDialog() {
		View view = LinearLayout.inflate(this, R.layout.activity_add_you_hui, null);
		this.et_quan_ma = (EditText) view.findViewById(R.id.et_quan_ma);
		this.et_quan_ma.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (count == 1) {
					if ((s.length() + 1) % 5 == 0) {
						YouHuiMaActivity.this.et_quan_ma.setText(s + " ");
						if (s.length() < 18) {
							YouHuiMaActivity.this.et_quan_ma.setSelection(s.length() + 1);
						} else {
							YouHuiMaActivity.this.et_quan_ma.setSelection(s.length());
						}
					}
				} else if (count == 0 && s.length() > 0 && s.length() % 5 == 0) {
					YouHuiMaActivity.this.et_quan_ma.setText(s.subSequence(0, s.length() - 1));
					YouHuiMaActivity.this.et_quan_ma.setSelection(s.length() - 1);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		Builder builder = new Builder(this);
		builder.setView(view);
		builder.setPositiveButton("\u786e\u5b9a", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				YouHuiMaActivity.this.dialogAddQuan.dismiss();
				if (YouHuiMaActivity.this.et_quan_ma.getText().toString() == null
						|| YouHuiMaActivity.this.et_quan_ma.getText().toString().replace(" ", "").trim().length() <= 0) {
					MainToast.show(YouHuiMaActivity.this, "\u8bf7\u8f93\u5165\u4f18\u60e0\u7801\uff01", 0);
				} else {
					YouHuiMaActivity.this.addQuanMa();
				}
			}
		});
		builder.setNegativeButton("\u53d6\u6d88", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				YouHuiMaActivity.this.dialogAddQuan.dismiss();
			}
		});
		return builder.create();
	}

	private void addQuanMa() {
		Map<String, String> map = new HashMap();
		map.put("code", this.et_quan_ma.getText().toString().replace(" ", "").trim());
		NetUtil.post(this, Constant.YOUHUIGETQUAN, map, new RequestCallBack<String>() {
			@Override
			public void onStart() {
				super.onStart();
				YouHuiMaActivity.this.dialogLoading.show();
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				YouHuiMaActivity.this.dialogAddQuan.dismiss();
				try {
					JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
					if (jsonObject.getInt("code") == 10000) {
						MainToast.show(YouHuiMaActivity.this, "\u6dfb\u52a0\u6210\u529f\uff01", 0);
						YouHuiMaActivity.this.initData();
						return;
					}
					YouHuiMaActivity.this.dialogLoading.dismiss();
					MainToast.show(YouHuiMaActivity.this, jsonObject.getString("error"), 0);
				} catch (Exception e) {
					e.printStackTrace();
					YouHuiMaActivity.this.dialogLoading.dismiss();
					MainToast.show(YouHuiMaActivity.this, "\u670d\u52a1\u5668\u5fd9", 0);
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				YouHuiMaActivity.this.dialogLoading.dismiss();
				YouHuiMaActivity.this.dialogAddQuan.dismiss();
				MainToast.show(YouHuiMaActivity.this, YouHuiMaActivity.this.getResources().getString(R.string.netFail),
						0);
			}
		});
	}
}
